#!/python-32
# -*- coding: utf-8 -*-
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is DataBank.
#
# The Initial Developer of the Original Code is
# Samarth Agarwal.
# Portions created by the Initial Developer are Copyright (C) 2011
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
#***** END LICENSE BLOCK *****
#
#*****START VARIABEL BLOCK*****
#file = the database/shelf file
#db = the database/shelf object 
#tapp = to be appended/recorded to the database
#keys = a list of all the database's keys
#flag = if True = that record/rey already exists, if False = that record/rey does not exist
#Name = 1st field
#note = 2nd field
#strkey=str(key)
#*****END VARIABEL BLOCK*****



#******Imports all the modules it needs(V)*****
import shelve
from PythonCard import model
import easygui
import os
#******Imports all the modules it needs(^)*****
Version = 1.0
#
License = open("License.txt", "r")

#***********The main program(V)****************************************************************************************3        
class MainWindow(model.Background):
    #self.components.del.visible = False
    #self.components.del.enabled = False 
    def on_About_command(self, event):
        License = open("License.txt", "r")
        easygui.textbox(msg="About DataBank\nVersion %i \nCopyright 2011 Samarth Agarwal" % Version, title="DataBank|About", text=License)
        License.close()

#***********Creates a new db/opens db/Saves DB(V)*************  
    
    def on_Newdb_command(self, event):
        global db
        file = easygui.filesavebox(msg = "Where do you want to save your database??",  title = "DATABANK|NewDb", default = "*.dat", filetypes = ["*.dat"] )
        try:
            db = shelve.open(file, writeback=True)
            keys = db.keys()
            self.components.Keys.items = keys
        except:
            pass
        
    def on_Opendb_command(self, event):
        global db        
        file = easygui.fileopenbox(msg = "What database do you want to open??",  title = "DATABANK|OpenDB", default = "*.dat", filetypes = ["*.dat"] )
        try:
            db = shelve.open(file, writeback=True)
            keys = db.keys()
            self.components.Keys.items = keys 
        except:
            pass
        
    
    
    
    def on_Savedb_command(self, event):
        
        global db
        try:
            db.sync()
            easygui.msgbox(msg="Sync Sucssful",  title="Sync|DataBank")
                    
            keys = db.keys()
            self.components.Keys.items = keys
        except:
            easygui.exceptionbox(msg="An ERROR occurred ,if this error continues to occurred then file a issue on: http://code.google.com/p/data-bank/ and reproduce the error message below:", title="ERROR|DataBank")
     

    
            
    
        


#***********Creates a new db/opens db(^)*************    


#******************Saves A record/Opens a recored/Creates a record(V)    
    def on_Savekey_command(self, event):
        global db        
        key = self.components.No.text
        Name = self.components.name.text
        note = self.components.notes.text
        tapp =[Name, note]
        strkey = str(key)
        flag = db.has_key(strkey)#checks if the record already exists
        
        if flag == True:#if record exists then it asks wheder to overwrite,change new record,change old record 
            e=easygui.buttonbox(msg="That Key Already Exists, are you Sure you want to overwrite",  title="DataBank|Overwrite",  choices =["Overwrite", "Nope",])
            if e == "Overwrite":
                global db
                strkey = str(key)
                key = self.components.No.text
                Name = self.components.name.text
                note = self.components.notes.text
                strkey = str(key)
                tapp =[Name, note]
                db[strkey] = tapp
                keys = db.keys()
                self.components.Keys.items = keys   
            elif e == "Nope":
                pass
          
        else:
            global db
            strkey = str(key)
            key = self.components.No.text
            Name = self.components.name.text
            note = self.components.notes.text
            strkey = str(key)
            tapp =[Name, note]
            db[strkey] = tapp
            keys = db.keys()
            self.components.Keys.items = keys 
        
    


 


    
    def on_OpenKey_command(self, event):
             
        topen = self.components.key.text
        
        topens = str(topen)
        
        try:
            global db
            copy = db[topens]
            Nom = copy[0]
            note = copy[1]
            self.components.No.text = topens
            self.components.notes.text = note
            self.components.name.text = Nom

        except KeyError:
            easygui.msgbox(msg="Not Found", title = "DATABANK|404")
    
        
    def on_Newkey_command(self, event):
        self.components.No.text = ""
        self.components.notes.text = ""
        self.components.name.text = ""
        
    

#******************Saves A record/Opens a recored/Creates a record(^)
    
            
#******The main program(^)*********************************************************************************************3

app = model.Application(MainWindow) #The apps instance
try:
    app.MainLoop() #the main loop
except:
    easygui.exceptionbox(msg="An ERROR occurred ,if this error continues to occurred then file a issue on: http://code.google.com/p/data-bank/ and reproduce the error message below:", title="ERROR|DataBank")